﻿/// <reference path="~/Scripts/lib/toastr.js" />
/// <reference path="mock/mock.js" />
/// <reference path="~/Scripts/lib/infuser.js" />
/// <reference path="~/Scripts/lib/knockout-2.2.0.js" />

/*
 @title: config.js
 @description: Contain application configurations, and predefined key-value fields. 
 */
define('config',
    ['toastr', 'mock/mock', 'infuser', 'ko'],
    function (toastr, mock, infuser, ko) {
        var
            // TODO: remove in production. Looks like some debug information
            currentUserId = 3,
            // TODO: Current user information (Should not be just one field, but specific class with user specific settings)
            currentUser = ko.observable(),
            // TODO: hash routes (Should not be defined like this. This information should takes from database in first request)
            hashes = {
                workspace: '#/workspace',
            },
            // TODO: Remove in production (Need to define better way how to notify users)
            logger = toastr,
            // TODO: Error or notification messages (Should be moved in separate JS file, will be generates on first request)
            messages = { viewModelActivated: 'viewmodel-activation' },

            // Not clear what it is
            stateKeys = { lastView: 'state.active-hash' },

            // TODO: instead of one value, need to define a different rules how to store different pieces of information
            storeExpirationMs = (1000 * 60 * 60 * 24), // 1 day
            throttle = 400,
            // application title, does not make sence to store it here
            title = 'TimeTracker > ',
            // settings information
            toastrTimeout = 2000,

            // TODO: Should be removed in production
            _useMocks = false,
            useMocks = function (val) {
                if (val) {
                    _useMocks = val;
                    init();
                }
                return _useMocks;
            },

            // TODO: Also should be taken from backend, according to application configuration
            viewIds = {
                workspace: '#workspace-view'
            },

            // TODO: All such information should be moved in separate file (Like resource file)
            toasts = {
                justForTesting: 'This text just for testing of toastr functionality',
                errorGettingData: 'Could not retrieve data.  Please check the logs.'
            },

            // Not clear why it's empty
            dataserviceInit = function () { },

            // Knockout validation settings
            validationInit = function () {
                ko.validation.configure({
                    registerExtenders: true,
                    messagesOnModified: true,
                    insertMessages: true,
                    parseInputAttributes: true,
                    writeInputAttributes: true,
                    messageTemplate: null,
                    decorateElement: true
                });
            },

            // Configuration of rules how to find specific template 
            configureExternalTemplates = function () {
                infuser.defaults.templatePrefix = "_";
                infuser.defaults.templateSuffix = ".tmpl.html";
                infuser.defaults.templateUrl = "/Tmpl";
            },

            init = function () {
                if (_useMocks) {
                    dataserviceInit = mock.dataserviceInit;
                }
                // Why we need to call this function, it's not initialized??
                dataserviceInit();

                toastr.options.timeOut = toastrTimeout;
                configureExternalTemplates();
                validationInit();
            };
        init();

        return {
            currentUserId: currentUserId,
            currentUser: currentUser,
            dataserviceInit: dataserviceInit,
            hashes: hashes,
            logger: logger,
            messages: messages,
            stateKeys: stateKeys,
            storeExpirationMs: storeExpirationMs,
            throttle: throttle,
            title: title,
            toasts: toasts,
            useMocks: useMocks,
            viewIds: viewIds,
            window: window
        };
    });